home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: c.koontz@ix.netcom.com(Curtis Alan Koontz)
- Newsgroups: comp.lang.c
- Subject: Help with SORTING!!!!
- Date: 23 Mar 1996 22:05:18 GMT
- Organization: Netcom
- Message-ID: <4j1siu$4dc@dfw-ixnews2.ix.netcom.com>
- NNTP-Posting-Host: vic-ca1-19.ix.netcom.com
- X-NETCOM-Date: Sat Mar 23 4:05:18 PM CST 1996
-
- Hello out there in C land.
-
- I am in need of help getting my sort algorithm below working.
- everything seem to work alright but the "if ... then" statement.
- I had it working with arrays but cannot get it working with link
- lists. not all of the class is included just what you might need
- to help me find my ERROR or bug. I thank you one and all for any
- help you can offer me to solve my problem. I did not include node
- greate (new) or delete functions.
-
- Last node 'next' and first 'node' prev is set to NULL. I can
- traverse the list with no trouble. When the cur_->d.t.Grade
- change all the future nodes change to that record.
-
- If someone has some linklist classes and are willing to share
- with me, I will be very thankful. The one below is my first try
- at linklists.
-
- System: 386-25 w/4meg. using Borland C/C++ 3.1, large memory
- module. Will be porting to linux for use very soon.
-
- One more question. I found the hard way and have read thereafter.
- That when a node is delete or freed the memory is returned to
- program but is not available for new nodes to be greated from.
- can some explain why this is so? Example: the program I am making
- read a list of ticks then load the stock prices into another list
- or array. The price record count change (30-4000) for each tick.
- If I delete the list and make a new one for the current record
- count the memory from the deleted list is not used for the new
- list, so I quickly run out of memory in DOS.
-
- // code I need help with below
-
- struct tickinfo {
- int count;
- char group;
- long loc;
- TickNameStruct t;
- };
- typedef tickinfo tickdata;
-
- struct StructNode { //StructNode
- tickdata d;
- StructNode *next;
- StructNode *prev;
- }; // NodeStruct;
- typedef StructNode NodeStruct;
-
-
- class classtick {
- NodeStruct *cur_, *first_, *last_, *temp_;
- int node_;
- short _cmptick(void) {return(strcmp(cur_->d.t.TickS,
- temp_->d.t.TickS));}
- short _cmpgrade(void) {return(strcmp(cur_->d.t.Grade,
- temp_->d.t.Grade));}
- public:
- NodeStruct *first(void) {return(first_);}
- NodeStruct *last(void) {return(last_);}
- NodeStruct *getnode(NodeStruct *first, int num);
- NodeStruct *getnode(int num) {return(getnode(first(), num));}
- int nodecount(void);
- int nodes(void) {return(node_);}
- void sortlist(void);
- };
-
- int classtick::nodecount(void) {
- int cnt = 0;
- for(temp_=first(); temp_!=last()->next; temp_=temp_->next)
- ++cnt;
- node_ = cnt; //set count of max nodes in list
- return(cnt);
- }
-
-
-
- void classtick::sortlist(void) { //HELP WITH SORTING!!!!
- NodeStruct *xcur, *Tcur;
- for(cur_=first(); cur_!=NULL; cur_=cur_->next) { //working OK
- printf("check for dup's on %s %s\n", cur_->d.t.TickS,
- cur_->d.t.Grade);
- for(temp_=cur_; temp_!=NULL; temp_=temp_->next)
- if (!_cmptick() && !_cmpgrade() && (temp_!=cur_))
- killnode(temp_);
- }
- int ii=0, bottom, top, middle, j;
- nodecount(); //node correct count for max nodes
- for(ii=1; ii<nodes(); ii++) {
- temp_ = getnode(ii);
- printf("sorting on %s %s\n", temp_->d.t.TickS, temp_->d.t.Grade);
- bottom = 0;
- top = ii-1;
- while (bottom<=top) {
- middle = (bottom+top)/2;
- cur_ = getnode(middle); //working OK in tests
- if ((_cmptick()>0) || (_cmpgrade()>0)) //this not working <<<
- top = middle-1;
- else
- bottom = middle+1;
- }
- for(j=ii-1;j>=bottom;j--) {
- cur_ = getnode(j);
- xcur = cur_->next;
- xcur->d = cur_->d;
- }
- cur_ = getnode(bottom);
- cur_->d = temp_->d;
- }
- }
-
-